/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean.event;

import assisisw.smartcal.entitysubsystem.Event;
import assisisw.smartcal.entitysubsystem.IEventController;
import assisisw.smartcal.entitysubsystem.User;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;

/**
 *
 * @author Francesco
 */
@ManagedBean ( eager = true )
@Named ( "editEventControllerBean" )
@ViewScoped ()
public class EditEventControllerBean extends AbstractEventControllerBean 
{
    
    // Instance Methods
    
    /***/
    public EditEventControllerBean () {}
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void confirmAction() 
    {
        editEvent () ;
    }
    
    /***/
    @Override
    public void undoAction() 
    {
    }
    
    /***/
    @Override
    public void deleteAction () 
    {
        Event e ;
        IEventController ec ;
        e = getManagedEvent () ;
        ec = getEventController () ;
        ec.delete(e); 
        getDashboardBean ().onEventDeleted ( e ) ;
    }

    /**
     * @param selectEvent 
     */
    public void onEventSelect ( SelectEvent selectEvent ) 
    {
        DefaultScheduleEvent scheduleEvent ;
        Event e ;
        scheduleEvent = getDashboardBean().onEventSelect ( selectEvent ) ;
        e = ( Event ) scheduleEvent.getData () ;
        setAvailableCalendars ( getDashboardBean().getCalendars () ) ;
        setManagedEvent ( e ) ;
        setEditAllowed ( getDashboardBean().getManagedUser ().getOwnedCalendars().contains( e.getOwningCalendar () ) );
        setTimeConsistencyInfo(""); 
        RequestContext r = RequestContext.getCurrentInstance();
        if ( e.getVisibility().trim().compareToIgnoreCase(Event.PRIVATE_VISIBILITY ) != 0 || e.getOwningCalendar().getOwner().equals(getDashboardBean().getManagedUser()) )
            r.execute("PF('dashboardEditEventDialog').show()");
    }

    /***/
    private void editEvent () 
    {
        Event e ;
        IEventController ec ;
        e = getManagedEvent () ;
        ec = getEventController () ;
        ec.update ( e ) ; 
        getDashboardBean ().onEventUpdated ( e ) ;
    }

    @Override
    protected void effectiveSharing ( User u )
    {
        getEventController ().invite ( getManagedEvent(), u ) ;
    }
    
}
